package com.leetcode.p100;

import com.bean.TreeNode;
import com.utils.TreeUtils;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 广度
 */
public class Test129_dfs {

    public int sumNumbers(TreeNode root) {
        int sum = 0;
        if (root == null) {
            return sum;
        }

        Queue<TreeNode> queueTreeNode = new LinkedList<>();
        Queue<Integer> queueValue = new LinkedList<>();
        queueTreeNode.offer(root);
        queueValue.offer(root.val);

        while (!queueTreeNode.isEmpty()) {
            TreeNode node = queueTreeNode.poll();
            int num = queueValue.poll();

            TreeNode left = node.left;
            TreeNode right = node.right;
            if (left == null && right == null) {
                sum += num;
            } else {
                if (left != null) {
                    queueTreeNode.offer(left);
                    queueValue.offer(num * 10 + left.val);
                }

                if (right != null) {
                    queueTreeNode.offer(right);
                    queueValue.offer(num * 10 + right.val);
                }
            }
        }

        return sum;
    }

    public static void main(String[] args) {
        Test129_dfs test = new Test129_dfs();

        TreeNode treeNode = TreeUtils.buildTree("1,2,3");
        int result = test.sumNumbers(treeNode);
        System.out.println(result);
    }

}
